*******************************************************************************
*Replication File: Spain Analysis
*******************************************************************************
*For use with Spain.dta
*Variable information can be found in Codebook tab Spain

********************************************************************************
*Data preparation
********************************************************************************

//creating sample countries for averaging - Dropping missing
bysort scode: gen country = 1 if _N >= 7 
replace country = . if scou == "GEO" | scou == "SRB" | scou == "TUR" 
//Average citizenship by country
bys year SpainVote: egen ave_cit = mean(citacq) if country == 1
//Average citizenship by country for Linked Origins
bys year SpainVote: egen ave_citSC = mean(citacq) if ///
	country == 1 & (colony == 1 | comlang_ethno == 1 | SCAmerica == 1)
//Total citizenship by country
bys year SpainVote: egen t_cit = sum(citacq) if country == 1	
//Total citizenship by country Linked Origins
bys year SpainVote: egen t_citSC = sum(citacq) if ///
	country == 1 & (colony == 1 | comlang_ethno == 1 | SCAmerica == 1)
//For graphing
gen origin = "All"
replace origin = "South America" if SCAmerica == 1 & ///
	country == 1 & (colony == 1 | comlang_ethno == 1 | SCAmerica == 1)

********************************************************************************
*Data analysis
********************************************************************************

xtset scode year
*Baseline Model
eststo main: qui xtreg IHS_citacq municipal lpermres i.scode ///
	if year <= 2012, robust cluster(scode)
*Baseline Model with covariates
eststo yearfx: qui xtreg IHS_citacq municipal i.year i.scode lpermres ///
	Ll1sgdp l1sunemp spolity2  ///
	if year <= 2012, robust cluster(scode)
*Baseline Model with covariates - linked origins
eststo ties: qui xtreg IHS_citacq municipal i.year lpermres i.scode ///
	Ll1sgdp l1sunemp spolity2 ///
	if year <= 2012 & ///
	(colony == 1 | comlang_ethno == 1 | SCAmerica == 1), robust cluster(scode)
*Difference in Differences
eststo did_all: qui xtreg IHS_citacq SpainVote DiD time2010 i.year lpermres ///
	Ll1sgdp l1sunemp spolity2 colony comlang_ethno ///
	Asia Europe SCAmerica NAmerica Africa MENA ///
    if year <= 2012, ///
	robust cluster(scode)
*Difference in Differences - linked origins
eststo did_ties: qui xtreg IHS_citacq SpainVote DiD time2010 lpermres  ///
	Ll1sgdp l1sunemp spolity2 colony comlang_ethno i.year ///
	SCAmerica ///
    if year <= 2012 & ///
	(colony == 1 | comlang_ethno == 1 | SCAmerica == 1), ///
	robust cluster(scode)
	
*Difference in Differences - Robust: Origin Fixed Effect
eststo Rdid_all: qui xtreg IHS_citacq SpainVote DiD time2010 i.year lpermres ///
	Ll1sgdp l1sunemp spolity2 colony comlang_ethno SCAmerica ///
	i.scode ///
    if year <= 2012, ///
	robust cluster(scode)
*Difference in Differences Linked Origins - Robust: Origin Fixed Effect
eststo Rdid_ties: qui xtreg IHS_citacq SpainVote DiD time2010 i.year lpermres ///
	Ll1sgdp l1sunemp spolity2 colony comlang_ethno ///
	i.scode ///
    if year <= 2012 & ///
	(colony == 1 | comlang_ethno == 1 | SCAmerica == 1), ///
	robust cluster(scode)
*Difference in Differences - Robust: Boostrapped errors
eststo Rdid_allBoot: qui xtreg IHS_citacq ///
	SpainVote DiD time2010 i.year lpermres ///
	Ll1sgdp l1sunemp spolity2 colony comlang_ethno SCAmerica ///
    if year <= 2012, ///
	vce(bootstrap, reps(1000) seed(1013)) cluster(scode)
*Difference in Differences Linked Origins - Robust: Boostrapped errors
eststo Rdid_tiesBoot: qui xtreg IHS_citacq SpainVote DiD time2010 lpermres  ///
	Ll1sgdp l1sunemp spolity2 colony comlang_ethno i.year if year <= 2012 & ///
	(colony == 1 | comlang_ethno == 1 | SCAmerica == 1), ///
	vce(bootstrap, reps(1000) seed(1013)) cluster(scode)
	
********************************************************************************
*Appendix A.1.8
********************************************************************************

sort SpainVote year
graph set window fontface "Helvetica"

twoway (line t_cit year if SpainVote == 1 & year >= 2008, ///
		lcolor(blue) lwidth(0.6)) ///
	(line t_cit year if SpainVote == 0 & year >= 2008, ///
		lcolor(black) lwidth(0.6)), ///
	xline(2010, lcolor(red) lwidth(.5)) ///
	xline(2011, lcolor(red) lpat(dash) lwidth(.2))	///
	xline(2009, lcolor(red) lpat(dash) lwidth(.2))	///
	xscale(range(2008(1)2012)) xlabel(none) xtitle(" ")	///
	yscale(range(10000(15000)105000)) ylabel(10000 "10" 25000 "15" 40000 ///
		"40" 55000 "55" 70000 "70" 85000 "85" 100000 "100" 115000 "115" ///
		130000 "130", labsize(small)) ///
	legend(order(1 "Enfranchised" 2 "Excluded") lcolor(black) ///
		size(small) row(1) bmargin(0)) ///
	ytitle(" ", size(medsmall))  graphregion(color(white)) ///
	plotregion(lcolor(black) lwidth(med)) ylab(, glcolor(gs15)) ///
	title("Total", color(black) size(large) ///
		box bexpand  bcolor(gs13) lcolor(black)) ///
		name(tot1, replace)
		
twoway (line t_citSC year if SpainVote == 1 & year >= 2008, ///
		lcolor(blue) lwidth(0.6)) ///
	(line t_citSC year if SpainVote == 0 & year >= 2008, ///
		lcolor(black) lwidth(0.6)), ///
	xline(2010, lcolor(red) lwidth(.5)) ///
	xline(2011, lcolor(red) lpat(dash) lwidth(.2))	///
	xline(2009, lcolor(red) lpat(dash) lwidth(.2))	///
	xscale(range(2008(1)2012)) xlabel(none) xtitle(" ")	///
	yscale(range(10000(15000)105000) alt) ylabel(10000 "10" 25000 "15" 40000 ///
		"40" 55000 "55" 70000 "70" 85000 "85" 100000 "100" 115000 "115" ///
		130000 "130", labsize(small)) ///
	legend(order(1 "Enfranchised" 2 "Excluded") lcolor(black) ///
		size(small) row(1) bmargin(0)) ///
	ytitle(" ", size(medsmall))  graphregion(color(white)) ///
	plotregion(lcolor(black) lwidth(med)) ylab(, glcolor(gs15)) ///
	title("Total", color(black) size(large) ///
		box bexpand  bcolor(gs13) lcolor(black)) ///
		name(totSC, replace)
		
twoway (line ave_cit year if SpainVote == 0 & year >= 2008, ///
		lcolor(black) lwidth(0.6)) ///
	(line ave_cit year if SpainVote == 1 & year >= 2008, ///
		lcolor(blue) lwidth(0.6)), ///
	xline(2010, lcolor(red) lwidth(.5)) ///
	xline(2011, lcolor(red) lpat(dash) lwidth(.2))	///
	xline(2009, lcolor(red) lpat(dash) lwidth(.2))	///
	xscale(range(2008(1)2012)) xlabel(2008(1)2015, labsize(small) ///
		angle(45)) xtitle(" ") ///
	yscale(range(0(2000)22000)) ylabel(0 "0" 3000 "3" 6000 ///
		"6" 9000 "9" 12000 "12" 15000 "15" 18000 "18" 21000 "21" ///
		24000 "24", labsize(small)) ///
	legend(off)	///
	ytitle(" ", size(medsmall))  graphregion(color(white)) ///
	plotregion(lcolor(black) lwidth(med)) ylab(, glcolor(gs15)) ///
	title("Average", color(black) size(large) ///
		box bexpand  bcolor(gs13) lcolor(black)) ///
	legend(region(lcolor(white))) ///
		name(ave1, replace)
		
twoway (line ave_citSC year if SpainVote == 0 & year >= 2008, ///
		lcolor(black) lwidth(0.6)) ///
	(line ave_citSC year if SpainVote == 1 & year >= 2008, ///
		lcolor(blue) lwidth(0.6)), ///
	xline(2010, lcolor(red) lwidth(.5)) ///
	xline(2011, lcolor(red) lpat(dash) lwidth(.2))	///
	xline(2009, lcolor(red) lpat(dash) lwidth(.2))	///
	xscale(range(2008(1)2012)) xlabel(2008(1)2015, labsize(small) ///
		angle(45)) xtitle(" ") ///
	yscale(range(0(2000)22000) alt) ylabel(0 "0" 3000 "3" 6000 ///
		"6" 9000 "9" 12000 "12" 15000 "15" 18000 "18" 21000 "21" ///
		24000 "24", labsize(small)) ///
	legend(off) ///
	ytitle(" ", size(medsmall))  graphregion(color(white)) ///
	plotregion(lcolor(black) lwidth(med)) ylab(, glcolor(gs15)) ///
	title("Average", color(black) size(large) ///
		box bexpand  bcolor(gs13) lcolor(black)) ///
	legend(region(lcolor(white))) ///
		name(aveSC, replace)
		
graph combine tot1 ave1, ///
	title("All Origins", color(white) bexpand box bcolor(black) ///
		lcolor(white) lwidth(1)) ///
	graphregion(color(white) margin(0)) row(2) ///
	name(combined_all, replace)
	
graph combine totSC aveSC, ///
	title("Historically Linked Origins", color(white) ///
		bexpand box bcolor(black) lcolor(white) lwidth(1)) ///
	graphregion(color(white) margin(0)) row(2) ///
	name(combined_SC, replace)
		
graph combine combined_all combined_SC, ///
	l2title("Naturalisation by Origin (in 1000s)", color(black) size(small)) ///
	r2title(" ") ///
	graphregion(color(white) margin(0)) row(1) ///
	name(combined, replace)
	
graph export FigureA18.png, replace

********************************************************************************
*Appendix Table A.1.7
********************************************************************************

sum municipal lpermres ///
	Ll1sgdp l1sunemp spolity2 colony comlang_ethno ///
	Asia Europe SCAmerica NAmerica Africa Oceania MENA ///
	if lpermres!=. & IHS_citacq!=. & year<=2012
	
********************************************************************************
*Appendix Table B.1.1
********************************************************************************

esttab main yearfx ties did_all did_ties using AppendixB11.tex, se(3) b(3) ///
	nogaps star(+ 0.1 * 0.05 ** 0.01) ///
	refcat(Ll1sgdp "{\i Origin Covariates}" , nolabel) ///
	drop(Asia Europe NAmerica Africa MENA *.year *.scode) ///
	order(municipal SpainVote time2010 DiD lpermres ///
		Ll1sgdp l1sunemp spolity2 colony comlang_ethno) ///
	label nonumbers mtitles("1" "2" "3" "4" "5") ///
	addnotes("Unstandardized beta coefficients with robust clustered standard errors in parentheses." ///
	"+p < 0.10 * p < 0.05, ** p < 0.01.") nonotes replace

********************************************************************************
*Appendix Table C.1.1
********************************************************************************
	
esttab Rdid_allBoot Rdid_tiesBoot Rdid_all Rdid_ties using AppendixC11.tex, se(3) b(3) ///
	nogaps star(+ 0.1 * 0.05 ** 0.01) ///
	refcat(Ll1sgdp "{\i Origin Covariates}" , nolabel) ///
	drop(*.year *.scode) ///
	order(SpainVote time2010 DiD lpermres ///
		Ll1sgdp l1sunemp spolity2 colony comlang_ethno) ///
	label nonumbers mtitles("1a" "2a" "3" "4") ///
	addnotes("Unstandardized beta coefficients with robust clustered standard errors in parentheses." ///
	"{\i a} Bootstraps (n = 1000) are resampled with replacement based on clusters" ///
	"+p < 0.10 * p < 0.05, ** p < 0.01.") nonotes replace

********************************************************************************
*Figure 2
********************************************************************************

graph set window fontface "Helvetica" 
label variable Ll1sgdp "Log GDP pc{subscript:t-1}"
label variable l1sunemp "Unemployment{subscript:t-1}"

coefplot (main, label(All Origins) ///
		drop(_cons colony comlang_ethno *scode) xline(0) color("0 90 50") ///
		msymbol(0) msize(vsmall) levels(95 90) ///
		ciopts(lcolor("0 90 50" "0 90 50") lwidth(.2 .4)) citop offset(.4)) ///
	(yearfx, label(All Origins) ///
		drop(_cons colony comlang_ethno *scode *year) xline(0) color("35 139 69") ///
		msymbol(O) msize(vsmall) levels(95 90) ///
		ciopts(lcolor("35 139 69" "35 139 69") lwidth(.2 .4)) citop offset(.2)) ///
	(ties, label(Historical Legacy Origins) ///
		drop(_cons colony comlang_ethno *scode *year) xline(0) color("65 171 93") ///
		msymbol(d) msize(vsmall) levels(95 90) ///
		ciopts(lcolor("65 171 93" "65 171 93") lwidth(.2 .4)) citop offset(0)) ///
	(did_all, label(All Origins) ///
		drop(_cons colony comlang_ethno *year ///
		Asia Europe SCAmerica NAmerica Africa MENA) xline(0) color("116 196 118") ///
		msymbol(O) msize(vsmall) levels(95 90) ///
		ciopts(lcolor("116 196 118" "116 196 118") lwidth(.2 .4)) citop offset(-.2)) ///
	(did_ties,  label(Historical Legacy Origins) ///
		drop(_cons colony comlang_ethno *year ///
			Asia Europe SCAmerica NAmerica Africa MENA) ///
		color("161 217 155") ///
		msymbol(d) msize(vsmall) levels(95 90) ///
		ciopts(lcolor("161 217 155" "161 217 155") lwidth(.2 .4)) citop offset (-.4)), ///
	xline(0, lcolor(gs5)) ciopts(recast(rcap)) ///
	xlabel(, labsize(vsmall) labcolor(gs5)) ///
	ylabel(, glcolor(gs15) ang(0) labsize(vsmall) labcolor(gs5)) ///
	legend(order (1 "All Origins, w/o covariates" ///
		7 "Historical Origins, w/covariates{superscript:+}" ///
		4 "All Origins, w/covariates{superscript:+}"  ///
		13 "Historical Origins, w/covariates{superscript:+}" ///
		10 "All Origins, w/covariates{superscript:+}") ///
		size(vsmall) col(2) title({bf:Model}, size(vsmall) pos(12) color(gs5)) ///
		region(lcolor(gs5)) color(gs5)) ///
	grid(glcolor(gs12) glpattern("dot")) ///
	graphregion(color(white) lcolor(gs5)) yscale(noline) ///
	plotregion(lcolor(gs5)) ///
	order(municipal DiD lpermres SpainVote time2010) ///
	coeflabels(lpermres = "Permanent Residence (ln)" ///
		interaction10 = "Enfranchised X Resigistration" ///
		spolity2 = "Polity", wrap(30) notick labsize(vsmall) labcolor(gs5)) ///
	headings(SpainVote = "{bf:Difference in Differences}" ///
		i.year = "{bf:Year}" ///
		Ll1sgdp = "{bf:Origin Covariates}" ///
		2009.year = "{bf:Year}" ///
		interaction10 = "{bf: Interaction}") ///
	title(" " "{bf:Predicting Spanish Citizenship Acquisition by Municipal Suffrage}" " " ///
		, color(gs5) size(3) position(12)) ///
	note(" " "Unstandardized beta coefficients with robust standard errors clustered by origin.", ///
	 position(5) size(tiny) color(gs5))

graph export Figure2.png, replace
